package com.hh.generator.core.generator;

import com.hh.generator.common.dto.ColumnEntity;
import com.hh.generator.common.dto.TableEntity;
import com.hh.generator.common.util.TableToClassConvertUtil;
import com.hh.generator.core.Generator;
import com.hh.generator.core.model.ClassModel;
import com.hh.generator.core.model.Config;
import com.hh.generator.core.model.FacadeImplModel;
import com.hh.generator.core.model.FacadeModel;
import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hh.generator.common.util.TableToClassConvertUtil.*;

public class FacadeImplGenerator extends AbstractTableBaseGenerator implements Generator {

    private String templateName = "FacadeImpl.java.vm";

    public FacadeImplGenerator(Config config, TableEntity tableEntity, List<ColumnEntity> columnEntityList) {
        super(config, tableEntity, columnEntityList);
    }


    @Override
    public String fileName() {
        return facadeImplClassName(config.getTablePrefix(), tableEntity.getTableName()) + ".java";
    }

    @Override
    public ClassModel buildModel() {

        String queryRequestClassName = queryRequestClassName(config.getTablePrefix(), tableEntity.getTableName());
        String queryRequestClassFullName = config.getRequestPackage().replace("xx", config.getModule()) + "." + queryRequestClassName;
        String addRequestClassName = addRequestClassName(config.getTablePrefix(), tableEntity.getTableName());
        String addRequestClassFullName = config.getRequestPackage().replace("xx", config.getModule()) + "." + addRequestClassName;
        String addResponseClassName = addResponseClassName(config.getTablePrefix(), tableEntity.getTableName());
        String addResponseClassFullName = config.getResponsePackage().replace("xx", config.getModule()) + "." + addResponseClassName;
        String updateRequestClassName = updateRequestClassName(config.getTablePrefix(), tableEntity.getTableName());
        String updateRequestClassFullName = config.getRequestPackage().replace("xx", config.getModule()) + "." + updateRequestClassName;
        String updateResponseClassName = updateResponseClassName(config.getTablePrefix(), tableEntity.getTableName());
        String updateResponseClassFullName = config.getResponsePackage().replace("xx", config.getModule()) + "." + updateResponseClassName;
        String deleteResponseClassName = deleteResponseClassName(config.getTablePrefix(), tableEntity.getTableName());
        String deleteResponseClassFullName = config.getResponsePackage().replace("xx", config.getModule()) + "." + deleteResponseClassName;

        String infoClassName = infoClassName(config.getTablePrefix(), tableEntity.getTableName());
        String infoClassFullName = config.getInfoPackage().replace("xx", config.getModule()) + "." + infoClassName;
        String entityClassName = className(config.getTablePrefix(), tableEntity.getTableName());

        String path = config.getFacadeBasePath() + "/facade/v1/" + StringUtils.uncapitalize(className(config.getTablePrefix(), tableEntity.getTableName()));
        String serviceClassName = TableToClassConvertUtil.serviceClassName(config.getTablePrefix(), tableEntity.getTableName());
        String serviceClassFullName = config.getServicePackage().replace("xx", config.getModule()) + "." + serviceClassName;
        String facadeClassName = facadeClassName(config.getTablePrefix(), tableEntity.getTableName());
        String facadeClassFullName = config.getFacadePackage().replace("xx", config.getModule()) + "." + facadeClassName;
        Set<String> imports = new HashSet<>();
        imports.add(serviceClassFullName);
        imports.add(facadeClassFullName);

        FacadeImplModel model = new FacadeImplModel();
        if (config.getMethods().contains("query")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("query" + entityClassName);
            method.setRequest(entityClassName + "QueryRequest");
            model.setQueryMethod(method);
            imports.add(config.getPageClassName());
            imports.add(infoClassFullName);
            imports.add(queryRequestClassFullName);
        }
        if (config.getMethods().contains("get")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("get" + entityClassName + "ById");
            model.setGetMethod(method);
            imports.add(infoClassFullName);
        }
        if (config.getMethods().contains("list")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("list" + entityClassName);
            method.setRequest(entityClassName + "QueryRequest");
            model.setListMethod(method);
            imports.add(infoClassFullName);
            imports.add(queryRequestClassFullName);
            imports.add(config.getListClassName());
        }
        if (config.getMethods().contains("add")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("add" + entityClassName);
            method.setRequest(entityClassName + "AddRequest");
            method.setResponse(entityClassName + "AddResponse");
            model.setAddMethod(method);
            imports.add(addRequestClassFullName);
            imports.add(addResponseClassFullName);
        }
        if (config.getMethods().contains("update")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("update" + entityClassName);
            method.setRequest(entityClassName + "UpdateRequest");
            method.setResponse(entityClassName + "UpdateResponse");
            model.setUpdateMethod(method);
            imports.add(updateRequestClassFullName);
            imports.add(updateResponseClassFullName);
        }
        if (config.getMethods().contains("delete")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("delete" + entityClassName);
            method.setResponse(entityClassName + "DeleteResponse");
            model.setDeleteMethod(method);
            imports.add(deleteResponseClassFullName);
        }


        model.setLowerEntityClassName(StringUtils.uncapitalize(entityClassName));
        model.setEntityId(model.getLowerEntityClassName() + "Id");
        model.setInfoClassName(infoClassName);
        model.setFacadeClassName(facadeClassName);
        model.setServiceClassName(serviceClassName);
        model.setLowerServiceClassName(StringUtils.uncapitalize(model.getServiceClassName()));
        model.setPath(path);
        model.setPackageName(config.getFacadeImplPackage().replace("xx", config.getModule()));
        model.setImports(imports.stream().sorted().collect(Collectors.toList()));
        model.setComment(tableEntity.getTableComment() + " Facade接口实现");
        model.setAuthor(config.getClassAuthor());
        model.setDate(config.getDate());
        model.setClassName(facadeImplClassName(config.getTablePrefix(), tableEntity.getTableName()));
        model.setClassFullName(model.getPackageName() + "." + model.getClassName());
        return model;
    }

    @Override
    public String getTemplateName() {
        return templateName;
    }

}
